const query=require("../db/mysql")
const {md5}=require("utility")
const jwt=require("jsonwebtoken")
class listLogin{
    zhuce=async (ctx)=>{
        const {user,pass}=ctx.request.body
        console.log(user,pass)
        const isHavesql=`select * from login2 where user="${user}" `
        const isHave=await query(isHavesql)
        if(isHave.length){
            ctx.body={
                data:"账号存在"
            }
            return
        }

        const sql=`insert into login2(user,pass) values("${user}","${md5(String(pass))}") `
        const data=await query(sql)
        if(data.affectedRows===1){
            ctx.body={
                data:"注册成功"
            }
        }else{
            ctx.body={
                data:"注册失败"
            }
        }
    }



    denglu=async (ctx)=>{
        const {user,pass}=ctx.request.body
        const sql=`select * from login2 where user="${user}" and pass="${md5(String(pass))}"`
        const data=await query(sql)
        if(data.length){
            ctx.body={
                data:data[0],
                token:jwt.sign({id:data[0].id},"hjw")
            }
        }else{
            ctx.body={
                data:"登陆失败"
            }
        }
    }
}


module.exports=new listLogin()